#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.common import exter_attack
from common.common_models import ActionResult
from common.const import ExecuteResultEnum, BackupTypeEnum, RepositoryDataTypeEnum
from common.logger import Logger
from db2.comm.util.job_decorators import job_exception_decorator
from db2.comm.backup_param_parse import ParameterBackupParse
from db2.comm.util.common_util import Db2CommonUtil
from db2.comm.error_code import Db2ErrCode
from db2.backup.util.db2_backup_util import Db2BackupUtil
from db2.backup.single.single_ts_backup import SingleTsBackup
from db2.backup.db2_backup_base import Db2BackupBase

LOGGER = Logger().get_logger(filename="db2.log")


class HadrTsBackup(Db2BackupBase):
    def __init__(self, task_name, pid, job_id, sub_job_id, param_dict):
        super().__init__(task_name, pid, job_id, sub_job_id, param_dict)
        self.param_util = ParameterBackupParse(pid, job_id, sub_job_id, param_dict)
        self.backup_handle = SingleTsBackup(task_name, pid, job_id, sub_job_id, param_dict)

    def allow_backup_in_local_node(self):
        LOGGER.info("Start hadr allow_backup_in_local_node")
        user_name = self.param_util.get_now_nodes_user_name()
        db_name = self.param_util.get_ts_backup_database_name()
        if not Db2CommonUtil.check_is_primary(user_name, db_name):
            return Db2CommonUtil.build_action_result(
                code=ExecuteResultEnum.INTERNAL_ERROR.value, message="No primary nodes")
        return self.backup_handle.allow_backup_in_local_node()

    def query_job_permission(self):
        user_name = self.param_util.get_now_nodes_user_name()
        return self.backup_handle.query_job_permission(user_name)

    def check_backup_job_type(self):
        backup_type = self.param_util.get_backup_type()
        if backup_type != BackupTypeEnum.FULL_BACKUP.value:
            tmp_path = self.param_util.get_repositories_path(
                RepositoryDataTypeEnum.CACHE_REPOSITORY.value)
            if not Db2BackupUtil.query_pre_copy_agentid_by_rpc_tool(self.param_util.get_applications(),
                                                                    self.job_id, self.sub_job_id, tmp_path):
                return Db2CommonUtil.build_action_result(code=ExecuteResultEnum.INTERNAL_ERROR.value,
                                                         body_err=Db2ErrCode.ERR_INC_TO_FULL)
        user_name = self.param_util.get_now_nodes_user_name()
        return self.backup_handle.check_backup_job_type(user_name)

    def exec_backup_pre_job(self):
        user_name = self.param_util.get_now_nodes_user_name()
        return self.backup_handle.exec_backup_pre_job(user_name)

    @job_exception_decorator(write_progress=True)
    def exec_backup(self):
        user_name = self.param_util.get_now_nodes_user_name()
        return self.backup_handle.exec_backup(user_name)

    def report_copy(self, bak_time):
        return self.backup_handle.report_copy(bak_time)

    def query_backup_copy(self):
        pass

    def exec_backup_post_job(self):
        return self.backup_handle.exec_backup_post_job()

    @exter_attack
    def report_progress(self):
        user_name = self.param_util.get_now_nodes_user_name()
        return self.backup_handle.report_progress(user_name)

    def output_action_result(self, action_ret: ActionResult):
        super(HadrTsBackup, self).output_action_result(action_ret)

    def abort_job(self):
        user_name = self.param_util.get_now_nodes_user_name()
        return self.backup_handle.abort_job(user_name)
